/* $Header: oven.h,v 2.2 01/10/02 10:30:32 przybyls Exp $ */

/***********************************************************************/
/* INCLUDE FILE NAME: oven.h                                           */
/* ==================                                                  */
/*                                                                     */
/* Author:              Lesley Freed                                   */
/*                                                                     */
/* COMMENT:  This is an include file which declares all of the         */
/*           functions and some of the constants used for the oven     */
/*           and zones.                                                */
/*                                                                     */
/***********************************************************************/

#ifndef OVEN_H
#define OVEN_H

/*****************************/
/*   TYPEDEFS                */
/*****************************/

/*****************************/
/*   MACROS                  */
/*****************************/

#define CLOSE_FLAP()                                                          \
{                                                                             \
   Flap_Status.flap_setpt = FLAP_CLOSED;                                      \
   Flap_Status.flap_accum = 0;                                                \
}

#define OPEN_FLAP()                                                           \
{                                                                             \
   Flap_Status.flap_setpt = FLAP_LIMIT;                                       \
   Flap_Status.flap_accum = FLAP_LIMIT << 16;                                 \
}

#define HALF_OPEN_FLAP()                                                      \
{                                                                             \
   Flap_Status.flap_setpt = FLAP_HALF_OPEN;                                   \
   Flap_Status.flap_accum = FLAP_HALF_OPEN << 16;                             \
}

#define JAM_TEMP_SETPT(pZone,setpt)                                           \
{                                                                             \
                                                                              \
   /* Only jam setpt if the zone is not tracking the oven setpt */            \
   if ( (BIT8)(( pZone )->on_mask & Active_Wkfile.zones_oven_track_mode) == 0 )     \
   {                                                                          \
      /* Set target setpt and filtered setpt to new setpt */                  \
                                                                              \
      ( pZone )->temp_setpt = ( setpt );                                      \
      ( pZone )->filt_temp_setpt = ( setpt ) << 8;                            \
      ( pZone )->filt_setpt_residual = 0;                                     \
   }                                                                          \
                                                                              \
}                                                                             \

#define SET_ZONE_DEMAND_TO_ZERO(pzone)                                         \
{                                                                              \
   pzone->zone_demand = 0;                                                     \
   ResetIntegral(pzone);                                                       \
}

#define SET_OVEN_DEMAND_TO_ZERO                                                \
{                                                                              \
   Oven_Table.zone_demand = 0;                                                 \
   Oven_Cryo_Status.cryo_demand = 0;                                           \
}

#define SET_OVEN_CRYO_OFF()                                                    \
{                                                                              \
   Zones_Image &= CRYO_OFF_MASK;                                               \
}

#define SET_OVEN_CRYO_ON()                                                     \
{                                                                              \
   Zones_Image |= CRYO_ON_MASK;                                                \
}

#define OVEN_IS_OFF  ( ( Zone_Status.zones_on_off & OVEN_ON_MASK ) == 0 )
#define CRYO_IS_OFF  ( Active_Wkfile.oven_cryo.cryo_on_off == OFF )

#define ZONE_NOT_AT_SETPT(pzone)                                               \
     ((( (pzone)->zone_temp - (pzone)->filt_temp_setpt ) > 256*64) ||          \
      ((INT32)( (pzone)->zone_temp - (pzone)->filt_temp_setpt ) < -256*64))    \

#define OVEN_AT_SETPT()                                                        \
     ((( Oven_Table.zone_temp - Oven_Table.filt_temp_setpt ) < 256*64) &&      \
      ((INT32)( Oven_Table.zone_temp - Oven_Table.filt_temp_setpt ) > -256*64))\

#define ZONE_AT_SETPT(pzone)                                                   \
     ((( (pzone)->zone_temp - (pzone)->filt_temp_setpt ) < 256*64) &&          \
      ((INT32)( (pzone)->zone_temp - (pzone)->filt_temp_setpt ) > -256*64))    \

#define CLEAR_OVEN_HOLD_OFF()                                                  \
{                                                                              \
   Zone_Status.state.flags.oven_hold_off_active = 0;                           \
   ClearOvenHoldOff();                                                         \
}

#define SET_CRYO_ACTIVE(pzone)                                       \
{                                                                    \
   Zone_Status.cryo_active |= (pzone)->on_mask;                      \
}

#define SET_CRYO_INACTIVE(pzone)                                     \
{                                                                    \
   Zone_Status.cryo_active &= ~((pzone)->on_mask);                   \
   if ((pzone)->config->cryo_installed)                              \
   {                                                                 \
      (pzone)->cryo_status->cryo_ready_count = 0;                    \
      (pzone)->cryo_status->cryo_not_ready_count = 0;                \
      (pzone)->cryo_status->cryo_accum  = 0;                         \
      (pzone)->cryo_status->cryo_demand = 0;                         \
   }                                                                 \
}

#define SET_CRYO_FULL_ON(pzone)                                           \
{                                                                         \
   (pzone)->cryo_status->cryo_accum  = (pzone)->config->cryo_limit;       \
   (pzone)->cryo_status->cryo_demand = (pzone)->config->cryo_limit >> 14; \
}


#define CALC_CRYO_LIMIT(pzone)                                        \
{                                                                     \
   /* maximum cryo accumulator value:  2.5 * cryo_period * 65536 */   \
   (pzone)->config->cryo_limit = (pzone)->config->cryo_period*163840; \
}

#define CALC_MAX_ON_CRYO_LIMIT(pzone)                                  \
{                                                                      \
   /* maximum on cryo accum: cryo_limit * max_cryo_on / cryo_period */ \
   (pzone)->config->cryo_max_on_limit = (pzone)->config->cryo_limit*   \
                                   (pzone)->config->max_cryo_on_count; \
   (pzone)->config->cryo_max_on_limit /= (pzone)->config->cryo_period; \
}


/*****************************/
/*   SYMBOLIC CONSTANTS      */
/*****************************/

/* Thermal/Adc Error Constants */

#define ERR_COUNT_MAX   250     /* Maximum value error count can be */
                                /* before a thermal fault occurs    */
#define ERR_INCREMENT   2       /* value added to error count          */
                                /* reduced from 10 to 2 in order to    */
                                /* pass conducted burst susceptibility */
                                /* JSD - 10/26/94                      */

/* Oven and Zone Filtering Constants */

#define FILTER_DELAY           5      /* Delay time before filters are in-  */
                                      /* initialized in 5.12 Hz count:      */
                                      /* High freq filter:  25/256 sec      */
                                      /* Low freq filter:   32/51.2 sec     */

#define HIGH_FREQ_FILTER_DELAY 2      /* Delay time for high freq filters   */

#define ADC_OFFSET_FILT_FACTOR 6      /* IIR filter const for fs = 51.2 Hz  */
#define ADC_OFFSET_FILT_MASK   0x003f /* and corresponding filter mask      */

#define LINE_SENSE_FILT_FACTOR 3      /* IIR filter const for fs = 51.2 Hz  */
#define LINE_SENSE_FILT_MASK   0x0007 /* and corresponding filter mask      */

#define DERIV_FILT_FACTOR      2      /* filter const to smooth PID derivative   */
                                      /* 2**2 => new_avg = 1/4 rdg + 3/4 old_avg */

#define OVEN_TRACK_SETPT_OFFSET (3*256*64) /* 3 deg C offset from oven setpt  */

/* Oven and Zone Control Constants */

#define FLAP_GAIN    0x07       /* flap position control gain               */
#define FLAP_LIMIT   0x39d      /* maximum Flap Setpt (925 in 2 MS bytes of */
                                /* flap_accum)                              */
#define FLAP_CLOSED  0          /* setpt for oven flap closed               */
#define FLAP_HALF_OPEN 0x1ce    /* flap half open setpt                     */

#define FLAP_TARGET  6          /* target oven demand for flap closing      */

#define FULL_POWER      0x80    /* nominal value for 100% duty cycle        */
#define MIN_POWER ( -2*FULL_POWER + 1)  /* min allowable calculated demand  */
#define MAX_POWER (  2*FULL_POWER - 1)  /* max allowable calculated demand  */
#define NOMINAL_POWER   0x80    /* nominal value for calculated line power  */
#define NOMINAL_VOLTAGE 173     /* nominal value for line voltage           */

#define K_HEATER     ((30*256)/128) /* oven heater gain constant              */
                                    /*  128 = nominal line power              */

#define K_FLAP_OPEN  54/2       /* oven loss constant, flap fully open      */
#define K_WALL_LOSS  30*2       /* oven loss constant, flap fully closed    */
#define K_AMB_OPEN   ((55+TEMP_OFFSET)*64) /* oven loss const, flap full open */
#define K_AMB_SHUT   ((0+TEMP_OFFSET)*64) /* oven loss const, flap full closed*/


/* Oven and Zones Limits and Initial Values */

#define EVAL_FREQ100            512     /* 5.12 Hz in 100th's of a Hz         */

#define SAFETY_ON_DELAY         (51*5)  /* 5 second ON delay for oven at 51 Hz*/
#define ZONE_READY_DELAY        (5*30)  /* small zone 30 second equib. time   */
                                        /* in 5 Hz control counts             */
#define CRYO_BLAST_READY_DELAY  (5*20)  /* cryo blast 20 second equib. time   */
                                        /* timeout after oven is within setpt */
#define OVEN_READY_DELAY        300     /* oven 3 minute equib. time in cmin  */

#define MAX_FULL_ON_TIME        (5*120) /* max full on time for CIS4: 2 min.  */
                                        /* in 5 Hz control counts             */

                      /* Maximum ready delay worst case timer count:          */
                      /* 999.99 min.s * 60 * 5.12 with MAX_TIME_SETPT in cmin */
#define MAX_READY_DELAY MAX_TIME_SETPT*6*EVAL_FREQ100/1000

/* Cryo Control Constants */

#define MIN_CRYO_ON_TICKS       4       /* minimum on count for cryo valve    */

#define CRYO_LIMIT      0x800000        /* maximum cryo accum value           */
#define CRYO_TARGET     8               /* 8/512's (about 1.5% duty cycle)    */

#define CRYO_AMBIENT_DEFAULT      25
#define CRYO_AMBIENT_MAX          55
#define CRYO_AMBIENT_MIN          -25   /* minimum enterable cryo ambient temp*/
                                        /* 8/20/96:  changed this to -25 to   */
                                        /*   support water cooling which does */
                                        /*   not want the oven flap to stop   */
                                        /*   operating until 0 C.             */


#define CRYO_AMBIENT_TEMP_DEFAULT (256*64*(CRYO_AMBIENT_DEFAULT+TEMP_OFFSET))
#define CRYO_AMBIENT_TEMP_MAX     (256*64*(CRYO_AMBIENT_MAX+TEMP_OFFSET))
#define CRYO_AMBIENT_TEMP_MIN     (256*64*(CRYO_AMBIENT_MIN+TEMP_OFFSET))

/* max time for cryo on when oven not ready and cryo on */

#define CRYO_FAULT_TIME       16            /* 16 min.s at full on for fault  */
#define MIN_TO_5_12HZ         (512/100*60)  /* # of 5.12 Hz time ticks per min*/

/* 16 min. full on for fault delay in 5.12 Hz time ticks */
#define CRYO_FAULT_SCALE_FACTOR MIN_TO_5_12HZ    /* Evaluate at 5.12 Hz rate  */
#define CRYO_FAULT_TIME_DELAY   (CRYO_FAULT_TIME*CRYO_FAULT_SCALE_FACTOR)

/* min/max time for cryo on when not RUN IN PROGRESS and oven ready  */
#define CRYO_TIMEOUT_DEF 30                      /*  30 min. default */
#define CRYO_TIMEOUT_MAX 120                     /* 120 min. maximum */
#define CRYO_TIMEOUT_MIN 10                      /* 10  min. minimum */
#define CRYO_TIMEOUT_SCALE_FACTOR  MIN_TO_5_12HZ /* Evaluate at 5.12 Hz rate  */

/* Oven and Zones Limits and Initial Values */

#define DEAD_TIME       30*5                          /* 30 sec equib. time   */
#define OVEN_TEMP_DEFAULT (64*(50+TEMP_OFFSET))       /* 50 degrees           */
#define ZONE_TEMP_DEFAULT (64*(250+TEMP_OFFSET))      /* 250 degrees          */
#define FPD_TEMP_DEFAULT  (64*(200+TEMP_OFFSET))      /* 200 degrees          */
#define AUX_TEMP_DEFAULT  (64*(150+TEMP_OFFSET))      /* 150 degrees  11/2/95 */

/* Oven and Zones Status Masks */

#define ALL_ZONES_PRESENT  0x7f     /* mask to configure in all thermal zones */
#define ALL_ZONES_NOT_READY 0x7f    /* mask to set all zones to not ready     */

/* Oven and zones user configuration masks */
#define ZONE_LIMITS_MASK   0x01     /* mask to set limits user modified bit   */
#define ZONE_TUNED_MASK    0x02     /* mask to set tuning parameters mod. bit */

/*****************************/
/*   FUNCTION PROTOTYPES     */
/*****************************/

/*  Declare Function Prototypes */

/* The following functions are used in startup so they should be in the boot ROM */
#pragma SECTION PROG=boot
        /* zone_init.c functions */
        void            InitZoneDefaultPresence( void );
        void            InitializeZone( ZONE_TABLE *pzone );
        void            InitializeFlap( void );
        void            InitializeOven_Zones( void );
        void            SetZoneDefaultSetpts( void );
        void            SetDefaultOvenTrackSetpt( pZONE_TABLE pzone );
        void            UpdateOven_ZoneStatus( void );
        void            InitZoneStores( void );
        void            InitPowerRelays( void );
        TEMP_SETPT      TempDefaultSetpt( pZONE_TABLE pzone );
        void            SetZonePFDefaultSetpts( pZONE_TABLE pzone );
        void            SetCryoPFDefaultSetpts( pZONE_TABLE pzone );
        void            CheckForThermalLimitChanges( pZONE_TABLE pzone );

        /* zone_config.c functions */
        void            DetermineZonesConfig( void );
        void            UpdateZoneInstalled( pZONE_TABLE pzone );
        void            SetZoneInstalled( pZONE_TABLE pzone, BIT8 installed );
        BIT8            GetZoneInstalled( pZONE_TABLE pzone );
        void            DetermineZoneInstalled( pZONE_TABLE pzone );
        void            ResetZonesConfig( void );
        void            ResetCryoConfig( pZONE_TABLE pzone );
        void            ResolveCryoConfig( void );
        void            GetTempSensorsStates( void );
        void            SetSensorPresence( pZONE_TABLE pzone, BIT8 presence );
        BIT8            GetSensorPresence( pZONE_TABLE pzone );
        BIT8            GetHeaterPresence( pZONE_TABLE pzone );
        void            SetHeaterWattage( pZONE_TABLE pzone, WATTAGE wattage );
        void            SetZoneRampCompType(pZONE_TABLE pzone, BIT8 ramp_comp);
        void            SetZoneConfig( BIT8 type,
                                       const ZONE_INFO *pinfo,
                                       ZONE_TABLE *pzone );
        void            DetermineInletZoneConfig( INLET_TYPE inlet_type,
                                                  ZONE_TABLE *pzone );
        void            CheckZonesConfig( void );
        void            DetermineDetZoneConfig( DET_TYPE det_type,
                                                ZONE_TABLE *pzone );
        void            DetermineAuxZoneConfig( AUX_TYPE aux_type,
                                                pZONE_TABLE pzone );

        void            InitAuxZonesConfig( void );
        void            DetermineOvenConfig( void );
        void            InitPwrFailOven_Zones ( void );
        void            SetInletControlRate( pZONE_TABLE pzone );
        void            SetAuxControlRate( pZONE_TABLE pzone );
        void            SetZoneMinIntegAccum(pZONE_TABLE pzone,INTEG_CLAMP type);
        void            SetZoneMaxIntegAccum(pZONE_TABLE pzone,INTEG_CLAMP type);
        void            SetZoneMinControlError(pZONE_TABLE pzone,INT8 min_error);
        void            SetZoneMaxControlError(pZONE_TABLE pzone,INT8 max_error);
        void            SetCryoConfig( pZONE_TABLE pzone );
        INT16           GetMinOvenActual( void );
        void            SetGCCryoType( void );
        void            SetCryoControlRate( pZONE_TABLE pzone, CRYO_TYPE cryo_type );

#pragma SECTION UNDO

        /* zone_tune.c functions */
        void       CalculateZoneTimePeriod( pZONE_TABLE pzone );
        void       CalculateZoneTunePropGain( pZONE_TABLE pzone );
        INT32      CalculateKu( pZONE_TABLE pzone, INT32 Ki );
        void       AbortZoneTuning( pZONE_TABLE pzone );
        void       ClearZoneTuning( void );

        /* zone_util.c functions */
        void            TurnOvenOff( void );
        void            TurnZoneOff( pZONE_TABLE zone );
        void            TurnZoneOn( pZONE_TABLE zone );
        void            TurnZoneOnOK( pZONE_TABLE zone );
        void            TurnOffZoneForShutdown( pZONE_TABLE pzone );
        void            ResetIntegral( pZONE_TABLE pzone );
        U_INT16         GetZoneShutdownMsg( void );
        BIT8            CheckZoneFaults( void );
        INT16           EvalOven( void );
        CRYO_SHUTDOWN   EvalCryo( pZONE_TABLE pzone );
        void            MonitorCryo( void );
        BIT8            GetThermalReadiness( void );
        void            EvalThermalReadiness( void );
        void            NoLineFreqFault( void );
        void            CheckForDeadMuxAdc( void );

        void            ShutdownZoneWithoutFlow ( pZONE_TABLE zone );
        void            ClearZoneShutdownWithoutFlow ( pZONE_TABLE zone );
        void            ShutdownCryo( pZONE_TABLE pzone );
        void            ClearAllZoneShutdowns(void);

#pragma SECTION PROG=SysCode
        void            CheckFalseLineFreqInterrupt( void );
#pragma SECTION UNDO
        void            ThermalShutdown( void );
        void            MonitorThermalSystem( void );

        /* oven.c functions */
        void            CalcOvenDemand( void );
        COOLING_MODE    ControlOvenCooling();
        U_INT16         CalcCryoDemand( pZONE_TABLE pzone );
        COOLING_MODE    ControlSmallZoneCooling( pZONE_TABLE pzone );
        COOLING_MODE    ControlForcedAirCooling( pZONE_TABLE pzone );
        COOLING_MODE    ControlCryoBangBang( pZONE_TABLE pzone );

        /* zone_task.c functions */
        void            ZoneTask( void );
        void            CalcDemand( void );

        /* line_freq.c functions */

#pragma SECTION PROG=SysCode
        void            LineFreq( void );
#pragma SECTION UNDO

        /* zone_pid.c functions */
        INT32           CalcPropGain( INT32 s_error );
        INT32           CalcIntegGain( INT32 s_error );
        INT32           CalcDerivGain( INT32 s_error );
        INT32           CalcPID( void );

        /* rtd_lin.c functions */
        U_INT16         LinRtd( pZONE_TABLE pzone );
        U_INT16         LinTC ( pZONE_TABLE pzone );     /* Gerstal */
        U_INT16         LinTc ( pZONE_TABLE pzone );     /* CPF     */

        /* zone_filt.c functions */

        U_INT16         LowFreqBoxCarSize( pZONE_TABLE pzone );
        U_INT16         Filter16U( pFILT_16U pfilter, U_INT16 value, U_INT16 size );
        void            BoxCarFilter( U_INT16 temp, pZONE_TABLE pzone );
        U_INT16         IIRFilter( pIIR_FILT_INFO pfilt,
                                   U_INT16 mask, U_INT8 shift );
        void            BoxCarFilterZoneRdgs( void );
#pragma SECTION PROG=boot
        void            InitializeZoneTemp( pZONE_TABLE pzone, U_INT16 rdg );
        void            ResetLowFreqFilter( ZONE_TABLE *pzone, U_INT16 rdg );
#pragma SECTION UNDO

        /* inlet_config.c functions */
        void            ResolveInletConfigs( void );

        /* zone_pwr.c functions */
        void            CalcPower( void );
        void            CheckZoneWattageConfig( void );
        void            ResolveZoneWattageConfig( void );
        void            DetermineZoneWattage( pZONE_TABLE pzone );
        void            OvenPowerTest( void );
#pragma SECTION PROG=SysCode
        void            ApplyOvenPowerSharingFactor( void );
#pragma SECTION UNDO

        /* flap.c functions */
#pragma SECTION PROG=SysCode
        void            FlapControl(void);
#pragma SECTION UNDO

        /* zone_diag.c functions */
        INT16           CalcTempDutyCycle( INT16 demand );
        void            InitTuneTable (ZONE_TYPE type);

#endif
